﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using Kaixin.BLL.Helper;
using System.IO;
using System.Text.RegularExpressions;
using Kaixin.BLL.Model;

namespace Kaixin.BLL.Func
{
    public class Firends
    {
        /// <summary>
        /// 好友的好友
        /// </summary>
        public List<User> FirendsFirend { get; set; }
        /// <summary>
        /// 我的好友
        /// </summary>
        public List<User> MyFirends { get; set; }

        #region 添加好友
        public string AddFriend(User user)
        {
            string url = "http://www.kaixin001.com/friend/addverify.php";
            string postData = "from=&touid={0}&content={1}&rcode=&code=";
            if (!MyFirends.Contains(user,new MyComparer()))
            {
                string html = WebHelper.Post(url, string.Format(postData, user.UID, "123"), "http://www.kaixin001.com/friend/new.php?touid=" + user.UID);
                List<GroupCollection> gc = RegexHelper.Matches(html, RegexHelper.AddFirendReturnType);

                return gc.Count == 0 ? "访问过快" : RegexHelper.Matches(html, RegexHelper.AddFirendReturnType)[0][1].Value;
            }
            else
            {
                return "2";//已经是好友
            }
        }
        #endregion
        private class MyComparer : IEqualityComparer<User>
        {
            #region IEqualityComparer<DictionaryEntry> 成员

            public bool Equals(User x, User y)
            {
                return x.UID == y.UID;
            }

            public int GetHashCode(User obj)
            {
                return obj.GetHashCode();
            }

            #endregion
        }

    }
}
